package br.eng.eliseu.gwt.aconchego.camera.server;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public final class EMF {
	
	private static final String UNIT_NAME = "persiste-Aconchego";  
    private static EntityManagerFactory FACTORY;  

    public static final ThreadLocal<EntityManager> SESSION = new ThreadLocal<EntityManager>();  
      
    public static EntityManager currentEntityManager() {  
        EntityManager manager = (EntityManager) SESSION.get();  
        if (manager == null) {    
            loadInstance();  
            manager = FACTORY.createEntityManager();  
            SESSION.set(manager);  
        }  
        return manager;  
    }  

    public static void closeEntityManager() {  
        EntityManager manager = (EntityManager) SESSION.get();  
        if (manager != null) {  
            manager.close();  
        }  
        SESSION.set(null);  
    }  

    private static synchronized void loadInstance() {  
        if (FACTORY == null) {  
            FACTORY = Persistence.createEntityManagerFactory(UNIT_NAME);  
        }  
    }  
	
	
}
